package com.tarena.demo.luban.all.main.controller;


import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.commons.restful.JsonResult;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import com.tarena.luban.demo.stock.api.DubboTestApi;
import com.tarena.luban.demo.stock.api.StockApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/order")
@Api(tags = "订单")
public class OrderController {

    //@Autowired
    //private OrderService orderService;
    @Autowired(required = false) //充分体现了RPC的底层框架特点:是看不出来是本地调用的还是远程调用的
    private DubboTestApi dubboTestApi;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @PostMapping("/add")
    @ApiOperation("新增订单的功能")
    public JsonResult addOrder(OrderAddParam orderAddParam){
        /*String result = dubboTestApi.sayHi("伊丽莎白");
        System.out.println("controller的调用者,拿到了返回值" + result);*/
        //orderService.addOrder(orderAddParam);

        Message<OrderAddParam> message = MessageBuilder.withPayload(orderAddParam).build();
        rocketMQTemplate.asyncSend("order-add-topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("成了");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("不成");
            }
        });

        //TODO 和前端的交互逻辑 变化了
        return JsonResult.ok("接收到新增订单!");
    }

}
